package com.Exception;

import java.util.HashMap;
import java.util.Map;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

/**
 * Created by HuangSuip on 2017/4/24.
 */
//@ControllerAdvice
public class SpringBootException {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object exception(Exception e, WebRequest request){
        Map<String ,Object> error = new HashMap<>();
        error.put("msg","some msg");
        error.put("exMsg",e.getMessage());
        error.put("id",request.getParameter("id"));
        return error;
    }

    @ModelAttribute
    public void atttibute(Model model){
        model.addAttribute("msg","other msg");
    }

    @InitBinder
    public void initBinder(WebDataBinder webDataBinder){
        webDataBinder.setDisallowedFields("id");
    }
}
